package com.example.bootdemo.service.impl;

import com.example.bootdemo.dao.EducationMapper;
import com.example.bootdemo.dao.EmployeeMapper;
import com.example.bootdemo.entity.Education;
import com.example.bootdemo.entity.Employee;
import com.example.bootdemo.entity.dto.EmployeeEntryDto;
import com.example.bootdemo.service.ExcelImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExcelImportServiceImpl implements ExcelImportService {

    @Autowired
    private EmployeeMapper employeeMapper;

//    @Autowired
//    private ProjectMapper projectMapper;

    @Autowired
    private EducationMapper educationMapper;

//    @Autowired
//    private OrganizationalStructureMapper organizationalStructureMapper;


    @Override
    public void insertExcel(List<List<EmployeeEntryDto>> allsheetList) {
        for (List<EmployeeEntryDto> list : allsheetList) {
            insert(list);
        }

    }
    @Transactional
    public void insert(List<EmployeeEntryDto> employeeEntryDtos) {
        //子类强转父类
        List<Employee> emplist = new ArrayList<>();
        emplist.addAll(employeeEntryDtos);
        //用于存放插入员工后返回的的id
        List<Integer> sheetEmpIds = new ArrayList<>();
        if (emplist.size() > 0) {
            //插入员工基本信息
            for (Employee e : emplist) {
//                e = ContentUtil.updateCloum(e);
                e.setRowState(0);
                List<Employee> employee = employeeMapper.selectByIDCard(e.getIdCardNumber());
                int i = 0;
                //根据身份证号码查找是否存在这个员工，如果不存在再插入，防止插入重复数据
                if (employee.size() == 0) {
                    Education education = new Education();
                    //插入教育信息
//                    if (e.getGraduationSchool() != null) {
//                        education.setEmployeeId(e.getId());
                        education.setQualificationCertificate(e.getQualificationCertificate());
                        education.setGraduationSchool(e.getGraduationSchool());
                        education.setSecondGraduationSchool(e.getSecondGraduationSchool());

                        education.setDegreeCertificateNumber(e.getDegreeCertificateNumber());
                        education.setSecondDegreeDiplomaNo(e.getSecondDegreeDiplomaNo());

                        education.setEducationalBackground(e.getEducationalBackground());
                        education.setSecondEducationalBackground(e.getSecondEducationalBackground());

                        education.setGraduationSchool(e.getGraduationSchool());
                        education.setSecondGraduationSchool(e.getSecondGraduationSchool());

                        education.setGraduationTime(e.getGraduationTime());
                        education.setSecondGraduationTime(e.getSecondGraduationTime());

                        education.setSpecialty(e.getSpecialty());
                        education.setSecondSpecialty(e.getSecondSpecialty());
//                        education = ContentUtil.updateCloum(education);
                        //添加教育信息
                        educationMapper.insert(education);
                        //在创建了教育信息之后需要进行绑定,
                        e.setEducationId(education.getId());
                        if(e.getEducationId()!=null){
                            employeeMapper.updateByPrimaryKey(e);
                        }
//                    }
                    //离职的员工进行导入，
                    e.setEducationId(education.getId());
                    i = employeeMapper.newInsertSelective(e);
                    /*if(e.getEmployeeStatus()!=5){
                        organizationalStructure =
                                organizationalStructureMapper.selectByPrimaryKey(e.getDepartmentId());

                        //对应岗位下的在职员工+1
                        organizationalStructureMapper.updateInservice(e.getDepartmentId(), 1);
                        //同时岗位绑定的部门的直属在职员工也需要+1
                        organizationalStructureMapper.updateInservice(organizationalStructure.getFatherId(), 1);
                    }*/
                }
                if (i == 1) {
                    sheetEmpIds.add(e.getId());
                }
            }

        }
    }
}
